#ifndef GY_GRAPHICSVIEWBP_H
#define GY_GRAPHICSVIEWBP_H

#include <QGraphicsView>
#include <QMenu>
#include <QJsonObject>
#include "GY_ControlLibrary_global.h"
class GY_PopupMenu;
class GY_DiagramScene;
class GY_ValuePanel;
class GY_DiagramItem;
/**
 * @brief 蓝图视图类
 */
class GY_CONTROLLIBRARY_EXPORT GY_GraphicsViewBP : public QGraphicsView
{
    Q_OBJECT
public:
    GY_GraphicsViewBP(QWidget *widget = nullptr);
    ~GY_GraphicsViewBP();

    GY_PopupMenu* GetMenu() const {return pMenu;}
    QMenu* GetItemMenu() const {return itemMenu;}
    void NewScene();  //空的场景
    void NewSceneSE();  //新的场景包含开始和结束图元
    GY_DiagramScene* GetScene() const {return scene;}
    int sceneItemCount();  //场景图元个数
    //ValuePanel* GetPanel() const {return panel;}
    void ShowPanel(GY_DiagramItem* item);  //显示设值面板
    void HidePanel();


    bool getRightBtnMoveRelease() const;

//方案相关
    void setSchemeFileDir(const QString &value);  //设置方案保存目录
    void setSchemeFileName(const QString &value);  //设置方案文件名
    QString getSchemeFilePath() const;  //获取方案文件路径
    void loadSchemeFile();  //加载方案文件
    QJsonObject& getSchemeFile();  //获取方案文件构造的json对象
    QJsonArray getSchemes();  //获取所有方案列表
    void loadScheme(QString groupName, QString schemeName);  //加载绘制方案
    bool addScheme(QString groupName, QString schemeName);  //增加新方案
    bool saveAsScheme(QString groupName, QString schemeName, QString saveAsSchemeName);  //另存新方案
    void resetScene();  //清空方案的场景，保留开始和结束图元, 需要手动保存方案
    void saveScheme(QString groupName, QString schemeName);  //保存方案

//场景图元设置相关
    void setBackgroundBrush(const QBrush &brush);  //设置场景背景
    void setForegroundBrush(const QBrush &brush);  //设置场景前景
    void setItemBgColor(const QColor & color);  //设置图元背景色
    void setItemTextColor(const QColor &value);  //设置图元文字颜色
    void setItemFontPixelSize(int value);  //设置图元字体像素大小

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    void contextMenuEvent(QContextMenuEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
signals:


private slots:
    void ShowPopupMenu(QPointF);
    void HidePopupMenu();
    void deleteItem();  //删除选中的所有图元
    void bringToFront();
    void sendToBack();
    void SetPortValue(QString);

private:
    GY_DiagramScene *scene;
    GY_PopupMenu* pMenu;  //自定义item类型选择菜单
    QMenu* itemMenu;  //item操作菜单
    GY_ValuePanel* panel;
    GY_DiagramItem *itemSel;  //当前选中图元
    bool ctrlKey;  //ctrl+wheel缩放
    QPoint rightBtnDownPos;  //右键位置
    bool rightBtnDown = false;  //右键按下
    bool rightBtnMove = false;  //右键移动
    bool rightBtnMoveRelease = false;  //右键移动释放

    //!相对中心点的位置
//    QPointF m_CenterAnchor;



//方案相关
    const QString configFileName = "./BlueprintConfig.json";  //配置文件名
    QJsonObject configFile;  //配置文件json对象
    QString schemeFileDir = "./modelFiles";  //方案保存目录, 须存到配置文件
    QString schemeFileName;  //方案文件名
    QJsonObject schemeFile;  //方案文件json对象

//场景图元设置相关
    QBrush sceneBgBrush = QBrush(QPixmap(":/images/background4.png"));  //场景背景
    QBrush sceneFgBrush = QColor(0,0,0,0);  //场景前景
    QColor itemBgColor = QColor(10, 10, 10, 200);  //图元背景颜色
    QColor itemTextColor = QColor(255,255,255);  //图元文字颜色
    int itemFontPixelSize = 12;  //图元字体像素大小
};

#endif // GY_GRAPHICSVIEWBP_H
